package com.hyh.admin.controller.resources;

import com.hyh.ad.common.core.controller.BaseController;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.core.page.TableDataInfo;
import com.hyh.ad.common.utils.SecurityUtils;
import com.hyh.admin.domain.resources.ResourcesCategory;
import com.hyh.admin.service.IResourcesCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/resources/sort")
@Api(tags = "资源分类管理")
public class ResourcesCategoryController extends BaseController {
    @Autowired
    private IResourcesCategoryService resourcesCategoryService;

    /*
     * 查询所有分类
     */
    @GetMapping("/list")
    @ApiOperation("查询所有分类")
    public TableDataInfo list(ResourcesCategory resourcesCategory) {
        startPage();
        return getDataTable(resourcesCategoryService.selectSortList(resourcesCategory));
    }

    /*
     * 查询子分类
     */
    @GetMapping("/list/{parentId}")
    @ApiOperation("查询子分类")
    public AjaxResult list(@PathVariable Long parentId) {
        return AjaxResult.success(resourcesCategoryService.selectResourcesByParentId(parentId));
    }

    /*
     * 删除当前分类
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除当前分类")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(resourcesCategoryService.deleteResourcesCategoryById(id));
    }

    /*
     * 新增分类
     */
    @PostMapping("/add")
    @ApiOperation("新增分类")
    public AjaxResult add(@RequestBody ResourcesCategory resourcesCategory) {
        resourcesCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(resourcesCategoryService.insert(resourcesCategory));
    }
}
